package cn.edu.cjw.APMFP.RoleRight.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.ResourceBundle;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;

import cn.edu.cjw.APMFP.RoleRight.pojo.RoleRight;
import cn.edu.cjw.APMFP.Util.HBaseUtill;
import cn.edu.cjw.APMFP.Util.HiveUtill;

public class RrDAOImpl implements RoleRightDAO {

	public boolean addAndUpdateRole(RoleRight r) throws Exception {

		// 调用HBaseUtill中的getConn方法
		// 获取HBase连接
		Connection conn = HBaseUtill.getConn();

		// 获取一个操作指定表的table对象，进行DML操作
		Table table = conn.getTable(TableName.valueOf(RoleRight.ROLE_TABLE_NAME));

		// 调用HBaseUtill中的putRole方法，传入RoleRight对象构造put
		Put putRole = HBaseUtill.putRole(r);

		// 插入表中
		table.put(putRole);

		// 释放资源
		HBaseUtill.Close(table, conn);

		return true;
	}

	public boolean removeRole(RoleRight r) throws Exception {

		Connection conn = HBaseUtill.getConn();

		Table table = conn.getTable(TableName.valueOf(RoleRight.ROLE_TABLE_NAME));

		// 调用HBaseUtill中的deleteRole方法，传入RoleRight对象构造delete
		Delete delete = HBaseUtill.deleteRole(r);

		table.delete(delete);

		HBaseUtill.Close(table, conn);

		return true;
	}

	/*
	 * 按编号搜索角色，也可判断角色是否存在 (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.cjw.APMFP.cjw.APMFP.RoleRight.DAO.RoleRightDAO#SearchRoleByrNUM(java.
	 * lang.String)
	 */
	public RoleRight SearchRoleByrNUM(String rNum) throws Exception {
		
		RoleRight roleRight = new RoleRight();

		//读取配置文件获取Hive数据
		ResourceBundle bundle = ResourceBundle.getBundle("System");

		String url = bundle.getString("Hive-url");
		String user = bundle.getString("Hive-user");
		String password = bundle.getString("Hive-password");

		//调用HiveUtill的建立连接方法，与hiveserver建立连接
		java.sql.Connection conn = HiveUtill.getConn(url, user, password);


		String sql = "SELECT * FROM apmfp_role_table WHERE rno=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, rNum);

		ResultSet rs = ps.executeQuery();

		while (rs.next()) {

			roleRight.setRNum(rs.getString("rno"));
			roleRight.setRName(rs.getString("rname"));
			roleRight.setRDescribe(rs.getString("rdescribe"));

			ArrayList<String> arrayList = new ArrayList<String>();
			String[] split = rs.getString("rright").split(",");
			for (String string : split) {
				arrayList.add(string);
			}
			roleRight.setRRight(arrayList);

		}

		HiveUtill.Close(rs, ps, conn);
		
		return roleRight;
	}

	public ArrayList<RoleRight> SearchRoleByName(String rName) throws Exception {

		//读取配置文件获取Hive数据
		ResourceBundle bundle = ResourceBundle.getBundle("System");

		String url = bundle.getString("Hive-url");
		String user = bundle.getString("Hive-user");
		String password = bundle.getString("Hive-password");

		//调用HiveUtill的建立连接方法，与hiveserver建立连接
		java.sql.Connection conn = HiveUtill.getConn(url, user, password);

		ArrayList<RoleRight> roleList = new ArrayList<RoleRight>();

		String sql = "SELECT * FROM apmfp_role_table WHERE rname=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, rName);

		ResultSet rs = ps.executeQuery();

		while (rs.next()) {

			RoleRight roleRight = new RoleRight();

			roleRight.setRNum(rs.getString("rno"));
			roleRight.setRName(rs.getString("rname"));
			roleRight.setRDescribe(rs.getString("rdescribe"));

			ArrayList<String> arrayList = new ArrayList<String>();
			String[] split = rs.getString("rright").split(",");
			for (String string : split) {
				arrayList.add(string);
			}
			roleRight.setRRight(arrayList);

			roleList.add(roleRight);
		}

		HiveUtill.Close(rs, ps, conn);
		
		return roleList;
	}

	public ArrayList<RoleRight> showAll() throws Exception {
		
		ArrayList<RoleRight> list = new ArrayList<RoleRight>();
		
		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String url = bundle.getString("Hive-url");
		String user = bundle.getString("Hive-user");
		String password = bundle.getString("Hive-password");
		
		
		//获取Hive连接
		java.sql.Connection conn = HiveUtill.getConn(url, user, password);
		
		//选取操作的数据对象
		String sql = "SELECT * FROM "+RoleRight.ROLE_TABLE_NAME;
		
		Statement cStatement = conn.createStatement();
		
		//执行SQL
		ResultSet rs = cStatement.executeQuery(sql);
		
		//对数据进行处理
		while(rs.next()) {
			
			String rNum = rs.getString("rno");
			String rName = rs.getString("rname");
			String rDescribe = rs.getString("rdescribe");
			
			String[] split = rs.getString("rright").split(",");
			ArrayList<String> rRight = new ArrayList<String>();
			for (String string : split) {
				rRight.add(string);
			}
			RoleRight roleRight = new RoleRight(rNum, rName, rDescribe, rRight);
			list.add(roleRight);
		}
		
		//释放资源
		HiveUtill.Close(rs, cStatement, conn);
		
		return list;
	}

}
